﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace chungcu
{
    public partial class dichvu : Form
    {
        private SqlConnection conn;
        private DataTable dt = new DataTable("dichvu");
        private DataTable dt1 = new DataTable("dkdichvu");
        private SqlDataAdapter da = new SqlDataAdapter();

        private void connect()
        {

            String cn = "Data source=(local);Initial Catalog=Quanlychungcu;Integrated Security=True";
            try
            {
                conn = new SqlConnection(cn);
                conn.Open(); 

            }
            catch (SqlException ex)
            {
                Console.WriteLine(ex.Message);
                MessageBox.Show("khong thanh cong");
            }
        }

        private void disconnect()
        {
            conn.Close();
            conn.Dispose();
            conn = null;
        }
        public dichvu()
        {
            InitializeComponent();
        }

        private void dichvu_Load(object sender, EventArgs e)
        {
            connect();
            getdata();
            binding();
            
        }

        private void getdata()
        {
            SqlCommand command = new SqlCommand(); 
            command.Connection = conn; 
            command.CommandType = CommandType.Text; 
            command.CommandText = @"select  dvid[Madv],dvtendichvu[Tên dịch vụ] from dichvu "; 
            da.SelectCommand = command;
            da.Fill(dt);
            showdichvu.DataSource = dt;
            cbbdichvu.DataSource = dt;
            cbbdichvu.DisplayMember = "dvtendichvu";
            cbbdichvu.ValueMember = "Madv";
            command.CommandText = @"select * from dkdichvu";
            da.SelectCommand = command;
            da.Fill(dt1);
            showdkdv.DataSource = dt1;
            cbbphong.DataSource = dt1;
            cbbphong.DisplayMember = "dkdvsophong";
            cbbphong.ValueMember = "dkdvsophong";
            cbbdichvu.DataBindings.Clear();
            cbbdichvu.DataBindings.Add("Text",showdkdv.DataSource,"dvid");
            
        }
        private void binding()
        {
            txtmadv.DataBindings.Clear();
            txtmadv.DataBindings.Add("Text",showdichvu.DataSource,"Madv");
            txttendv.DataBindings.Clear();
            txttendv.DataBindings.Add("Text",showdichvu.DataSource,"Tên dịch vụ");
            txtmadk.DataBindings.Clear();
            txtmadk.DataBindings.Add("Text", showdkdv.DataSource, "dkdvid");
        }
        private void btnthoat_Click(object sender, EventArgs e)
        {
            Close();
            Dispose();
           

        }

        private void btnthemdv_Click(object sender, EventArgs e)
        {
            nhapdichvu ndv = new nhapdichvu();
            ndv.Show();
            this.Close();
        }

        private void btnsua_Click(object sender, EventArgs e)
        {
            sddichvu sddv = new sddichvu();
            sddv.Show();
            this.Close();
        }

        private void btnxoa_Click(object sender, EventArgs e)
        {
            DataRow row1 = dt1.Select("dvid = " + Convert.ToInt32(cbbdichvu.Text))[0];
            row1.BeginEdit();
            row1.Delete();
            row1.EndEdit();
            SqlCommand commandDelete1 = new SqlCommand();
            commandDelete1.Connection = conn;
            commandDelete1.CommandType = CommandType.Text;
            commandDelete1.CommandText = @"delete from dkdichvu where dvid = @madv";
            commandDelete1.Parameters.Add("@madv", SqlDbType.Int, 20, "dvid");
            da.DeleteCommand = commandDelete1;
            da.Update(dt1);
            MessageBox.Show("Bạn đã xoá thành công !","Thông báo",MessageBoxButtons.OK);
        }

        private void btndkdv_Click(object sender, EventArgs e)
        {
            DataRow row = dt1.Select("dkdvid = " + Convert.ToInt32(txtmadk.Text))[0];
            row.BeginEdit();
            row["dkdvsophong"] = cbbphong.SelectedValue;
            row["dvid"] = cbbdichvu.SelectedValue;
            row.EndEdit();
            SqlCommand command = new SqlCommand();
            command.Connection = conn;
            command.CommandType = CommandType.Text;
            command.CommandText = @"update dkdichvu set dvid = @madv where dkdvid = @dkdvid";
            command.Parameters.Add("@madv",SqlDbType.Int,20,"dvid");
            command.Parameters.Add("@dkdvid",SqlDbType.Int,20,"dkdvid");
            da.UpdateCommand = command;
            da.Update(dt1);
            
            
            MessageBox.Show("Bạn đã thay đổi thành công !","Thông báo",MessageBoxButtons.OK);
        }

        private void btntim_Click(object sender, EventArgs e)
        {

            if (cbbselect.SelectedIndex == 0)
            {
                dt1.Clear();
                SqlCommand commandphong = new SqlCommand();
                commandphong.Connection = conn;
                commandphong.CommandType = CommandType.Text;
                commandphong.CommandText = @"Select * from dkdichvu where (dkdvsophong = @phong )";
                commandphong.Parameters.Add("@phong", SqlDbType.Int, 20).Value = txtselect.Text;
                da.SelectCommand = commandphong;
                da.Fill(dt1);
                showdkdv.DataSource = dt1;
            }
            else if (cbbselect.SelectedIndex == 1)
            {
                dt1.Clear();
                SqlCommand commandphong = new SqlCommand();
                commandphong.Connection = conn;
                commandphong.CommandType = CommandType.Text;
                commandphong.CommandText = @"Select * from dkdichvu where (dvid = @id )";
                commandphong.Parameters.Add("@id", SqlDbType.Int, 20).Value = txtselect.Text;
                da.SelectCommand = commandphong;
                da.Fill(dt1);
                showdkdv.DataSource = dt1;
            }
        }

        private void btnxoadv_Click(object sender, EventArgs e)
        {
            try
            {
                DataRow row = dt.Select("Madv = " + Convert.ToInt32(txtmadv.Text))[0];
                row.BeginEdit();
                row.Delete();
                row.EndEdit();
                SqlCommand commandDelete = new SqlCommand();
                commandDelete.Connection = conn;
                commandDelete.CommandType = CommandType.Text;
                commandDelete.CommandText = @"Delete from dichvu where dvid = @madv";
                commandDelete.Parameters.Add("@madv", SqlDbType.Int, 20, "madv");
                da.DeleteCommand = commandDelete;
                da.Update(dt);
            }
            catch (SqlException ex)
            {
                Console.WriteLine(ex.Message);
                MessageBox.Show("Phòng nào đó đang đăng ký dịch vụ này !!","Chú ý",MessageBoxButtons.OK);
            }
        }

              
    }
}
